package com.situ.student.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.student.dao.impl.KechengDaoImpl;
import com.situ.student.entity.Kecheng;
import com.situ.student.service.IKechengServlet;
import com.situ.student.service.IStudentService;
import com.situ.student.service.impl.KechengServletImpl;
import com.situ.student.service.impl.StudentServiceImpl;

public class KechengServlet extends HttpServlet {
	private IKechengServlet kechengservlet = new KechengServletImpl();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 解决乱码问题
		req.setCharacterEncoding("utf-8");
		String method = req.getParameter("method");
		if (method == null) {
			method = "list";
		}
		switch (method) {
		case "list":
			List(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "toupdate":
			toupdate(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		default:
			break;
		}

	}

	public void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// 先获取浏览器数据
		String kechengname = req.getParameter("kechengname");
		Integer xuefen = Integer.parseInt(req.getParameter("xuefen"));
		Kecheng kecheng = new Kecheng(kechengname, xuefen);
		// 调用service层方法
		boolean result = kechengservlet.insert(kecheng);
		// 重定向到界面
		resp.sendRedirect(req.getContextPath() + "/kecheng?method=list");
	}

	public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// 获取浏览器传过来的信息
		Integer id=Integer .parseInt(req.getParameter("id"));
		String kechengname = req.getParameter("kechengname");
		Integer xuefen = Integer.parseInt(req.getParameter("xuefen"));
		Kecheng kecheng = new Kecheng(id,kechengname, xuefen);
		// 调用service层方法
		boolean result = kechengservlet.update(kecheng);
		System.out.println(result ? true : false);
		// 转发或者重定向
		resp.sendRedirect(req.getContextPath() + "/kecheng?method=list");
	}

	public void toupdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Integer id = Integer.parseInt(req.getParameter("id"));// 括号里是浏览器name=的名字
		System.out.println(id);
		Kecheng kecheng = kechengservlet.findById(id);
		System.out.println(kecheng);
		// 转发到更新界面
		req.setAttribute("kecheng", kecheng);
		req.getRequestDispatcher("/kecheng_update.jsp").forward(req, resp);
	}

	public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// 获取ID
		Integer id = Integer.parseInt(req.getParameter("id"));
		boolean count = kechengservlet.deleteById(id);
		System.out.println(count ? true : false);
		// 重定向到界面
		resp.sendRedirect(req.getContextPath() + "/kecheng?method=list");
	}

	private void List(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Kecheng> list = kechengservlet.list();
		for (Kecheng kecheng : list) {
			System.out.println(kecheng);
		}
		// 转发到界面进行展示
		req.setAttribute("list", list);
		req.getRequestDispatcher("/kecheng_html_list.jsp").forward(req, resp);
	}
}
